﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using StarsAdmin.Application.Region.Dtos;
using StarsAdmin.Application.Role.Dtos;
using StarsAdmin.Core.Cache;
using StarsAdmin.Core.Consts;
using StarsAdmin.Core.DB.Entities;
using StarsAdmin.Core.Extensions;
using StarsAdmin.Core.Models;
using StarsAdmin.Repository.Address;

namespace StarsAdmin.Application.Region.Services
{
    public class RegionService : IRegionService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly IRegionRepository _regionRepository;

        public RegionService(IMapper mapper, ICacheService cacheService, IRegionRepository regionRepository)
        {
            _mapper = mapper;
            _cache = cacheService;
            _regionRepository = regionRepository;
        }

        #region 其他页面获取区域的接口

        public async Task<List<RegionDto>> GetRegionsAsync()
        {
            return await _cache.GetOrSetAsync(CacheKeyConst.RegionCacheKey, async () =>
            {
                var result = await _regionRepository.GetAll().AsNoTracking().ToTreeAsync();
                return _mapper.Map<List<RegionDto>>(result);
            }, TimeConst.FailureTimeMinute);
        }

        #endregion 其他页面获取区域的接口

        #region 查询

        public async Task<List<RegionDto>> GetAllAsync(BaseQueryInput input)
        {
            var result = await _regionRepository.GetAll().AsNoTracking()
                    .WhereIf(input != null && input.QueryConditions.Any(), input.QueryConditions)
                    .OrderByIf(input != null && input.Sort != null, input.Sort, input.Order != false)
                    .ToTreeAsync();
            return _mapper.Map<List<RegionDto>>(result);
        }

        public async Task<Page<RegionDto>> GetPageAsync(PageQueryInput input)
        {
            var result = await _regionRepository.GetAll().AsNoTracking()
                       .WhereIf(input != null && input.QueryConditions.Any(), input.QueryConditions)
                       .OrderByIf(input != null && input.Sort != null, input.Sort, input.Order != false)
                       .ToPageListAsync(input.CurrentPage, input.PageSize);
            return _mapper.Map<Page<RegionDto>>(result);
        }

        public async Task<RegionDto?> GetByIdAsync(long id)
        {
            var result = await _regionRepository.GetByIdAsync(id);
            return _mapper.Map<RegionDto>(result);
        }

        public async Task<List<RegionDto>> GetByIdsAsync(long[] ids)
        {
            var result = await _regionRepository.GetByIds(ids).ToListAsync();
            return _mapper.Map<List<RegionDto>>(result);
        }

        #endregion 查询

        #region 导入导出

        public async Task<FileContentResult?> ExportAsync(long[] ids)
        {
            var export = await _regionRepository.GetByIds(ids).ToListAsync();
            return await _mapper.Map<List<ExportRegionDto>>(export).ToExcelAsync("地址管理");
        }

        public async Task<bool> ImportAsync(List<AddRegionInput> inputs)
        {
            var entities = _mapper.Map<List<RegionEntity>>(inputs);
            var result = await _regionRepository.AddRangeAndSaveAsync(entities);
            if (result)
            {
                await _cache.RemoveAsync(CacheKeyConst.RegionCacheKey);
            }
            return result;
        }

        #endregion 导入导出

        #region 添加

        public async Task<bool> AddAsync(AddRegionInput input)
        {
            var entity = _mapper.Map<RegionEntity>(input);
            var result = await _regionRepository.AddAndSaveAsync(entity);
            if (result)
            {
                await _cache.RemoveAsync(CacheKeyConst.RegionCacheKey);
            }
            return result;
        }

        #endregion 添加

        #region 更新

        public async Task<bool> UpdateAsync(UpdateRegionInput input)
        {
            var entity = _mapper.Map<RegionEntity>(input);
            var result = await _regionRepository.UpdateAndSaveAsync(entity);
            if (result)
            {
                await _cache.RemoveAsync(CacheKeyConst.RegionCacheKey);
            }
            return result;
        }

        #endregion 更新

        #region 删除

        public async Task<bool> DeleteAsync(long id)
        {
            var result = await _regionRepository.DeleteAndSaveAsync(id);
            if (result)
            {
                await _cache.RemoveAsync(CacheKeyConst.RegionCacheKey);
            }
            return result;
        }

        public async Task<bool> BatchDeleteAsync(long[] ids)
        {
            var result = await _regionRepository.DeleteRangeAndSaveAsync(ids);
            if (result)
            {
                await _cache.RemoveAsync(CacheKeyConst.RegionCacheKey);
            }
            return result;
        }

        #endregion 删除
    }
}